home *** CD-ROM | disk | FTP | other *** search
/ Motor Sport Digital Archive Collection 1960s / Motor Sport Digital Archive Collection 1960s.iso / main.swf / scripts / com / glenpike / search / TextFileSearcher.as
Encoding:
Text File  |  2008-05-21  |  7.7 KB  |  269 lines

  1. package com.glenpike.search
  2. {
  3.    import com.glenpike.loader.TextLoader;
  4.    import events.SearchEvent;
  5.    import flash.events.ErrorEvent;
  6.    import flash.events.Event;
  7.    import flash.events.EventDispatcher;
  8.    
  9.    public class TextFileSearcher extends EventDispatcher
  10.    {
  11.       private var _cancelled:Boolean = false;
  12.       
  13.       private var _searchTerm:String;
  14.       
  15.       private var _ignoredWords:Array;
  16.       
  17.       private var _txtLoader:TextLoader;
  18.       
  19.       private var _currFile:int;
  20.       
  21.       private var _ignoredWordsFile:String = "data/ignored-words.txt";
  22.       
  23.       private var _results:Array;
  24.       
  25.       private var _files:Array;
  26.       
  27.       public function TextFileSearcher()
  28.       {
  29.          _cancelled = false;
  30.          _ignoredWordsFile = "data/ignored-words.txt";
  31.          super();
  32.          _init();
  33.       }
  34.       
  35.       private function _searchFor(param1:String) : void
  36.       {
  37.          var _loc2_:RegExp = null;
  38.          var _loc3_:Object = null;
  39.          var _loc4_:Array = null;
  40.          var _loc5_:Array = null;
  41.          var _loc6_:int = 0;
  42.          var _loc7_:int = 0;
  43.          var _loc8_:int = 0;
  44.          var _loc9_:Object = null;
  45.          _loc2_ = new RegExp(param1,"gi");
  46.          _results[_currFile] = new Object();
  47.          _results[_currFile].file = _files[_currFile];
  48.          _loc3_ = _loc2_.exec(_txtLoader.text);
  49.          if(null == _loc3_)
  50.          {
  51.             _results[_currFile].indexes = null;
  52.             return;
  53.          }
  54.          _loc4_ = new Array();
  55.          _loc5_ = pageIndexes();
  56.          _loc6_ = 0;
  57.          _loc7_ = int(_loc5_[_loc6_]);
  58.          do
  59.          {
  60.             _loc3_.count = 1;
  61.             while(_loc7_ < _loc3_.index)
  62.             {
  63.                _loc6_++;
  64.                _loc7_ = int(_loc5_[_loc6_]);
  65.             }
  66.             _loc3_.page = _loc6_;
  67.             if(null != _loc9_)
  68.             {
  69.                if(_loc3_.page == _loc9_.page)
  70.                {
  71.                   _loc3_ = _loc4_.pop();
  72.                   ++_loc3_.count;
  73.                }
  74.             }
  75.             else
  76.             {
  77.                _loc8_ = _loc6_;
  78.             }
  79.             _loc4_.push(_loc3_);
  80.             _loc9_ = _loc3_;
  81.          }
  82.          while(null != (_loc3_ = _loc2_.exec(_txtLoader.text)));
  83.          
  84.          _results[_currFile].indexes = _loc4_;
  85.          _results[_currFile].numPages = _loc5_.length;
  86.       }
  87.       
  88.       public function cancel() : void
  89.       {
  90.          _cancelled = true;
  91.       }
  92.       
  93.       private function _isIgnoredWord(param1:String) : Boolean
  94.       {
  95.          var _loc2_:Number = NaN;
  96.          trace("_isIgnoredWord ? " + param1);
  97.          if(null == _ignoredWords)
  98.          {
  99.             trace("no ignored words??!?!?!");
  100.             return false;
  101.          }
  102.          _loc2_ = _ignoredWords.length - 1;
  103.          while(_loc2_ >= 0)
  104.          {
  105.             trace(" - " + _ignoredWords[_loc2_]);
  106.             if(param1 == _ignoredWords[_loc2_])
  107.             {
  108.                return true;
  109.             }
  110.             _loc2_--;
  111.          }
  112.          return false;
  113.       }
  114.       
  115.       public function searchFor(param1:String, param2:Array) : Boolean
  116.       {
  117.          if(true == _invalidSearch(param1) || null == param2)
  118.          {
  119.             return false;
  120.          }
  121.          _searchTerm = param1;
  122.          _results = new Array();
  123.          _files = param2;
  124.          _currFile = 0;
  125.          _cancelled = false;
  126.          _txtLoader.addEventListener(Event.COMPLETE,_fileLoaded);
  127.          _txtLoader.addEventListener(ErrorEvent.ERROR,_fileLoaded);
  128.          dispatchEvent(new SearchEvent(SearchEvent.FILE_START,_files[_currFile]));
  129.          _txtLoader.loadFile(_files[_currFile]);
  130.          return true;
  131.       }
  132.       
  133.       private function _invalidSearch(param1:String) : Boolean
  134.       {
  135.          var _loc2_:Array = null;
  136.          var _loc3_:int = 0;
  137.          trace("_invalidSearch ? " + param1);
  138.          if(null == param1)
  139.          {
  140.             return true;
  141.          }
  142.          _loc2_ = param1.split(" ");
  143.          if(null == _loc2_)
  144.          {
  145.             trace("string split returned null???");
  146.             return true;
  147.          }
  148.          trace("check Ignored Words");
  149.          _loc3_ = int(_loc2_.length - 1);
  150.          while(_loc3_ >= 0)
  151.          {
  152.             if(false == _isIgnoredWord(_loc2_[_loc3_]))
  153.             {
  154.                return false;
  155.             }
  156.             _loc3_--;
  157.          }
  158.          return true;
  159.       }
  160.       
  161.       private function getSurroundingText(param1:String, param2:int) : *
  162.       {
  163.          var _loc3_:int = 0;
  164.          var _loc4_:int = 0;
  165.          var _loc5_:int = 0;
  166.          var _loc6_:int = 0;
  167.          var _loc7_:* = undefined;
  168.          var _loc8_:* = undefined;
  169.          var _loc9_:RegExp = null;
  170.          var _loc10_:String = null;
  171.          _loc3_ = 26 - param1.length;
  172.          if(0 >= _loc3_)
  173.          {
  174.             return param1;
  175.          }
  176.          _loc4_ = Math.floor(_loc3_ / 2);
  177.          _loc5_ = param2 - _loc4_;
  178.          _loc6_ = param2 + _loc4_;
  179.          if(0 > _loc5_)
  180.          {
  181.             _loc5_ = 0;
  182.          }
  183.          if(_loc6_ > _txtLoader.text.length)
  184.          {
  185.             _loc6_ = _txtLoader.text.length;
  186.          }
  187.          _loc7_ = _txtLoader.text.indexOf(" ",_loc5_);
  188.          if(-1 != _loc7_ && param2 > _loc7_ + 1)
  189.          {
  190.             _loc5_ = _loc7_ + 1;
  191.          }
  192.          _loc8_ = _txtLoader.text.lastIndexOf(" ",_loc6_);
  193.          if(-1 != _loc8_ && _loc6_ > _loc8_)
  194.          {
  195.             _loc6_ = _loc8_;
  196.          }
  197.          _loc9_ = /[\r\t\n]/gi;
  198.          return _txtLoader.text.substring(_loc5_,_loc6_).replace(_loc9_,"");
  199.       }
  200.       
  201.       private function _fileLoaded(param1:Event) : void
  202.       {
  203.          if(_cancelled)
  204.          {
  205.             dispatchEvent(new SearchEvent(SearchEvent.CANCELLED,null));
  206.             _txtLoader.removeEventListener(Event.COMPLETE,_fileLoaded);
  207.             _files = null;
  208.             _results = null;
  209.             return;
  210.          }
  211.          if(param1 instanceof ErrorEvent)
  212.          {
  213.             trace("error loading text " + ErrorEvent(param1).text);
  214.          }
  215.          else
  216.          {
  217.             trace("file loaded " + _files[_currFile]);
  218.             _searchFor(_searchTerm);
  219.             dispatchEvent(new SearchEvent(SearchEvent.FILE_COMPLETE,_files[_currFile]));
  220.          }
  221.          ++_currFile;
  222.          if(_currFile < _files.length)
  223.          {
  224.             dispatchEvent(new SearchEvent(SearchEvent.FILE_START,_files[_currFile]));
  225.             _txtLoader.loadFile(_files[_currFile]);
  226.          }
  227.          else
  228.          {
  229.             _txtLoader.removeEventListener(Event.COMPLETE,_fileLoaded);
  230.             dispatchEvent(new Event(Event.COMPLETE));
  231.          }
  232.       }
  233.       
  234.       private function _ignoredWordsLoaded(param1:Event) : void
  235.       {
  236.          _txtLoader.removeEventListener(Event.COMPLETE,_ignoredWordsLoaded);
  237.          _ignoredWords = _txtLoader.text.split(",");
  238.          trace("loaded ignored words " + _ignoredWords.length);
  239.       }
  240.       
  241.       private function _init() : void
  242.       {
  243.          _txtLoader = new TextLoader();
  244.          _txtLoader.addEventListener(Event.COMPLETE,_ignoredWordsLoaded);
  245.          _txtLoader.loadFile(_ignoredWordsFile);
  246.       }
  247.       
  248.       private function pageIndexes() : Array
  249.       {
  250.          var _loc1_:RegExp = null;
  251.          var _loc2_:Object = null;
  252.          var _loc3_:Array = null;
  253.          _loc1_ = /\f/gi;
  254.          _loc3_ = new Array();
  255.          while(null != (_loc2_ = _loc1_.exec(_txtLoader.text)))
  256.          {
  257.             _loc3_.push(_loc2_.index);
  258.          }
  259.          return _loc3_;
  260.       }
  261.       
  262.       public function get results() : Array
  263.       {
  264.          return _results;
  265.       }
  266.    }
  267. }
  268.  
  269.